package com.yunsc.admin.controller.content;

import com.yunsc.common.utils.PageResult;
import com.yunsc.common.utils.Result;
import com.yunsc.common.utils.SecurityUtil;
import com.yunsc.common.validator.ValidationGroups;
import com.yunsc.content.dto.ArticleCategoryDTO;
import com.yunsc.content.dto.ArticleDTO;
import com.yunsc.content.dto.QueryArticleDTO;
import com.yunsc.content.entity.YunscArticleCategoryEntity;
import com.yunsc.content.entity.YunscArticleEntity;
import com.yunsc.content.service.YunscArticleCategoryService;
import com.yunsc.content.service.YunscArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @version: 1.0
 * @Author: adan
 * @description:
 * @date: 2024-06-05 21:15
 */
@Api(tags = "文章内容管理")
@RestController
@RequestMapping("/content/article")
public class ArticleController {

    @Resource
    private YunscArticleService articleService;
    @Resource
    private YunscArticleCategoryService articleCategoryService;

    // /article/add
    @ApiOperation(value = "新增文章", notes = "新增文章")
    @PostMapping("/add")
    public Result<?> addArticle(@RequestBody ArticleDTO param) {
        Integer storeId = SecurityUtil.getCurrentStoreId();
        articleService.addArticle(param, storeId);
        return Result.ok("新增文章成功");
    }

    // /article/list
    @ApiOperation(value = "文章列表", notes = "文章列表")
    @GetMapping("/list")
    public Result<?> articleList(QueryArticleDTO param) {
        Integer storeId = SecurityUtil.getCurrentStoreId();
        PageResult<YunscArticleEntity> pageResult = articleService.articleList(param, storeId);
        return Result.resultPageList(pageResult);
    }

    // /article/delete

    @ApiOperation(value = "删除文章", notes = "删除文章")
    @PostMapping("/delete")
    public Result<?> deleteArticle(@RequestBody @Validated(ValidationGroups.Delete.class) ArticleDTO param) {
        articleService.deleteArticle(param.getArticleId());
        return Result.ok("删除文章成功");
    }

    // /article/edit

    @ApiOperation(value = "修改文章", notes = "修改文章")
    @PostMapping("/edit")
    public Result<?> updateArticle(@RequestBody @Validated(ValidationGroups.Update.class) ArticleDTO param) {
        articleService.updateArticle(param);
        return Result.ok("修改文章成功");
    }

    // /category/list

    @ApiOperation(value = "分类列表", notes = "分类列表")
    @GetMapping("/category/list")
    public Result<?> categoryList() {
        Integer storeId = SecurityUtil.getCurrentStoreId();
        List<YunscArticleCategoryEntity> categoryList = articleCategoryService.categoryList(storeId);
        return Result.resultList(categoryList);
    }

    // /category/add
    @ApiOperation(value = "新增分类", notes = "新增分类")
    @PostMapping("/category/add")
    public Result<?> addCategory(@RequestBody ArticleCategoryDTO param) {
        Integer storeId = SecurityUtil.getCurrentStoreId();
        articleCategoryService.addCategory(param, storeId);
        return Result.ok("新增分类成功");
    }

    // /category/edit
    @ApiOperation(value = "修改分类", notes = "修改分类")
    @PostMapping("/category/edit")
    public Result<?> updateCategory(@RequestBody @Validated(ValidationGroups.Update.class) ArticleCategoryDTO param) {
        articleCategoryService.updateCategory(param);
        return Result.ok("修改分类成功");
    }

    // /category/delete
    @ApiOperation(value = "删除分类", notes = "删除分类")
    @PostMapping("/category/delete")
    public Result<?> deleteCategory(@RequestBody @Validated(ValidationGroups.Delete.class) ArticleCategoryDTO param) {
        articleCategoryService.deleteCategory(param.getCategoryId());
        return Result.ok("删除分类成功");
    }
}
